// Project: the csa specific class libary
// File: ExtraFuncs
// Authors: CS.A classes, 2007 and rlb
// Date Started: 10.15.07 (about)
// Progress: reverseInt added 11.1.07
// Date Completed: never
// Notes:
// TODO: add more useful functions

package csa;

import java.lang.Math;
import general.DebugLog;

public class ExtraFuncs extends Object
    {
    public static long reverseLong(long n)
    // Returns: the value of an integer with its digits reversed
    //     for example: 32467 becomes 76423
    // int: any natural number; if the param is not > 0, zero is returned.
        {
        DebugLog.Assert(n > 0, "ExtraFuncs.reverseLong - param is too small.");
        long reversed = 0; // return 0 if input invalid
        while (n > 0)
            {
            reversed = 10 * reversed + n % 10;
            n /= 10;
            }
        return reversed;
        }
    
    public static int[] randomOrder(int number)
        {
        DebugLog.Assert(number > 0, "ExtraFuncs.randomOrder- param is too small.");
        int[] array = new int[number];
        for (int i = 0; i < array.length; i++)
            array[i] = i;
        for (int i = 0; i < array.length; i++)
            {
            int random = randUpTo(number);
            int temp = array[i];
            array[i] = array[random];
            array[random] = temp;
            }
        return array;
        }

    public static int randUpTo(int upperBound)
    // Returns: an integer of at least zero and no more than a given value;
    // the return value is uniformly randomly distributed.
    // int: the largest value to return
    // Desc: for example, randUpTo(100) would return some randomly selected
    //     integer x satisfying 0 <= x <= 100. To flip a coin, randUpTo(1)
        {

        DebugLog.Assert(upperBound > 0,
            "ExtraFuncs.randUpTo - upperbound is invalid.");
        upperBound = Math.max(upperBound, 1);
        return (int)((upperBound + 1) * Math.random());
        }
    public static int rand100()
     // Returns: a random integer between 1 and 100, inclusive.
        {
        return 1 + (int)(100 * Math.random());
        }
    } // class ExtraFuncs